package com.mengchengnet.chaperonage.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.TextView;

import com.mengchengnet.chaperonage.R;
import com.mengchengnet.chaperonage.domain.base.Address;

import java.util.HashMap;
import java.util.List;

/**
 * Created by Administrator on 2017/8/23.
 */

public class AddressAdapter extends BaseAdapter {
    private LayoutInflater mInflater = null;
    private List<Address> list;
    private Context mContext;
    private static HashMap<Integer, Boolean> isCheckMap =  new HashMap<Integer, Boolean>();

    public AddressAdapter(Context context, List<Address> lv)
    {
        //根据context上下文加载布局，这里的是Demo17Activity本身，即this
        this.mInflater = LayoutInflater.from(context);
        list=lv;
        mContext=context;
    }

    // 初始化isSelected的数据
    private void initDate(){
        for(int i=0; i<list.size();i++) {
            getIsSelected().put(i,false);
        }
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int i) {
        return list.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup viewGroup) {

        ViewHolder holder = null;
        //如果缓存convertView为空，则需要创建View
        if(convertView == null)
        {
            holder = new ViewHolder();
            //根据自定义的Item布局加载布局
            convertView = mInflater.inflate(R.layout.item_address, null);
           holder.name= (TextView) convertView.findViewById(R.id.item_address_name);
            holder.relationship= (TextView) convertView.findViewById(R.id.item_address_relationship);
            holder.address= (TextView) convertView.findViewById(R.id.item_address_address);
            holder.phone= (TextView) convertView.findViewById(R.id.item_address_phone);
            holder.others= (TextView) convertView.findViewById(R.id.item_address_others);
            holder.box= (CheckBox) convertView.findViewById(R.id.item_address_box);
            //将设置好的布局保存到缓存中，并将其设置在Tag里，以便后面方便取出Tag
            convertView.setTag(holder);
        }else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.relationship.setText(list.get(position).getRelationship());
        holder.name.setText(list.get(position).getName());
        holder.phone.setText(list.get(position).getPhone());
        holder.address.setText(list.get(position).getAddress());
        if (!list.get(position).getOthers().isEmpty()){
            holder.others.setVisibility(View.VISIBLE);
            holder.others.setText(list.get(position).getOthers());
        }
        holder.box.setTag(list.get(position).getPhone());
        //找到需要选中的条目
      if (list.get(position).isChecked()){
          holder.box.setChecked(true);
      }else{
          holder.box.setChecked(false);
      }
//   holder.box.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
//    @Override
//    public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
//        int radiaoId = Integer.parseInt(compoundButton.getTag().toString());
//        if(isChecked)
//        {
//            //将选中的放入hashmap中
//            isCheckMap.put(radiaoId, isChecked);
//            Log.e("333333333333", "onCheckedChanged: "+isChecked);
//        }
//        else
//        {
//            //取消选中的则剔除
//            isCheckMap.remove(radiaoId);
//            Log.e("333333333333", "onCheckedChanged: "+isChecked);
//        }
//    }
//   });


        return convertView;
    }


    public static class ViewHolder
    {

        public TextView name;
        public TextView relationship;
        public TextView address;
        public TextView phone;
        public TextView others;
        public CheckBox box;


    }

    public static HashMap<Integer,Boolean> getIsSelected() {
        return isCheckMap;
    }

    public static void setIsSelected(HashMap<Integer,Boolean> isSelected) {
        AddressAdapter.isCheckMap = isSelected;
    }
}
